import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * 文件工具类
 *
 * @author 张超
 */
public final class FileUtils {
  private static boolean unicodeExist;

  /**
   * 读取文件内容，并把内容作为字符串返回
   *
   * @param file 要读取的文件
   * @return 字符串形式的文件内容。
   */
  public static String readAsString(String file) {
    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
    FileUtils.unicodeExist = false;

    try {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
      String str = br.readLine();
      while (null != str) {
        String resStr = UicodeBackslashU.unicodeToCn(str);
        // 如果有unicode 编码字段
        if ( !FileUtils.unicodeExist && str != null && !str.equals(resStr)) {
          FileUtils.unicodeExist = true;
        }
        sb.append(resStr).append("\n");
        str = br.readLine();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (null != br) {
          br.close();
          br = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return sb.toString();
  }

  public static void WriteAsString(String filePath, String str) {
    BufferedWriter bw = null;
    try {
      File file = new File(filePath);
      PrintWriter out =
          new PrintWriter(
              new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8")));

      out.write(str);


      out.flush();
      out.close();
      out = null;
      file = null;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /** 得到当前的路径 */
  public static String getCurFolder() {
    String curPath = System.getProperty("user.dir");
    return curPath;
  }

  /** 遍历当前路径 对于每一个html 文件， 做 unicode 字符串转换 */
  public static void traverseFolder(String path) {

    File file = new File(path);
    if (file.exists()) {
      File[] files = file.listFiles();
      if (null == files || files.length == 0) {
        System.out.println("文件夹是空的!");
        return;
      } else {
        for (File file2 : files) {
          // 如果是文件夹
          if (file2.isDirectory()) {
            traverseFolder(file2.getAbsolutePath());
            // 如果是文件
          } else {
            String filePath = file2.getAbsolutePath();
            System.out.println("文件:" + filePath);
            // 如果是html 文件
            if (filePath.endsWith("html")) {
              System.out.println("     html文件:" + filePath);
              String str = FileUtils.readAsString(filePath);
              //               如果存在 unicode 编码字段
//              if (filePath.endsWith("Test.html")) {
//                System.out.println(str);
//              }
              if (FileUtils.unicodeExist) {
                System.out.println("     写回 html文件:" + filePath);
                FileUtils.WriteAsString(filePath, str);
              }
            }
          }
        }
      }
    } else {
      System.out.println("文件不存在!");
    }
  }
}
